package com.example.activelifecycletest.persistence.shared_perferences;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.activelifecycletest.R;

import java.util.Set;

/**
 * sharedP活动主类
 * @author zhangding
 * @date 2022年04月24日15:17:47
 */
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shared_perferencr_main);
        Button saveData = (Button) findViewById(R.id.save_data);
        saveData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 获取传输对象
                SharedPreferences.Editor editor = getSharedPreferences("data",MODE_PRIVATE).edit();
                // 输入数据
                editor.putString("name","zhangding");
                editor.putInt("age",18);
                editor.putBoolean("BIG",true);
                // 写入数据
                editor.apply();
            }
        });
        Button restoreData = (Button) findViewById(R.id.restore_data);
        restoreData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SharedPreferences sharedPreferences = getSharedPreferences("data",MODE_PRIVATE);
                String name = sharedPreferences.getString("name","");
                int age = sharedPreferences.getInt("age",0);
                Toast.makeText(MainActivity.this, "name:"+name+"\n age:"+age, Toast.LENGTH_SHORT).show();
            }
        });
    }
}